/*
 * ttbqt - A teletekst browser
 * http://code.google.com/p/ttbqt
 * Copyright (C) 2011, Kevin van der Vlist
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "Controller.h"

#include <QtDebug>

/**
 * \class Controller
 *
 * \short Controller implementing all slots for ButtonBar
 *
 * This class manages the control flow for the ButtonBar
 * by implementing its slots
 */

/**
 * \short Constructor of the Controller
 * 
 * \param pi The PageInfo
 * \param bb The ButtonBar
 *
 * Requires a pointer to the PageInfo and the ButtonBar class to
 * function correctly.
 */

Controller::Controller(PageInfo *pi, ButtonBar *bb) {
  pageinfo = pi;
  buttonbar = bb;
}

/**
 * \short Destructor of the controller
 *
 * Destructor of the controller
 */

Controller::~Controller() {

}

/**
 * \short Slot function for downPressed()
 * 
 * Handles the downPressed() signal from the ButtonBar
 */

void Controller::downPressed() {
  qDebug() << "downPressed();";
  // down equals previous sub page
  teleTextSubPageChanged(pageinfo->getSubPage()-1);
}

/**
 * \short Slot function for helpPressed()
 * 
 * Handles the helpPressed() signal from the ButtonBar
 */

void Controller::helpPressed() {
  qDebug() << "helpPressed();";
  HelpBox hb;
  hb.showHelp(buttonbar);
}

/**
 * \short Slot function for nextPressed()
 * 
 * Handles the nextPressed() signal from the ButtonBar
 */

void Controller::nextPressed() {
  qDebug() << "nextPressed();";
  teleTextPageChanged(pageinfo->getPage()+1);
}

/**
 * \short Slot function for prevPressed()
 * 
 * Handles the prevPressed() signal from the ButtonBar
 */

void Controller::prevPressed() {
  qDebug() << "prevPressed();";
  teleTextPageChanged(pageinfo->getPage()-1);
}

/**
 * \short Slot function for prevPressed()
 * 
 * Handles the upPressed() signal from the ButtonBar
 */

void Controller::upPressed() {
  qDebug() << "upPressed();";
  teleTextSubPageChanged(pageinfo->getSubPage()+1);
}

/**
 * \short Slot function for teleTextSubPageChanged()
 * 
 * \param value The value given.
 * 
 * Handles the teleTextSubPageChanged() signal from the ButtonBar
 */

void Controller::teleTextSubPageChanged(int value) {
  qDebug() << "teleTextSubPageChanged(" << value << ");";
  pageinfo->setSubPage(value);
  buttonbar->setSubPage(value);
  buttonbar->clearFocus();
}

/**
 * \short Slot function for teleTextPageChanged()
 * 
 * \param value The value given.
 * 
 * Handles the teleTextPageChanged() signal from the ButtonBar
 */

void Controller::teleTextPageChanged(int value) {
  qDebug() << "teleTextPageChanged(" << value << ");";
  pageinfo->setPage(value);
  buttonbar->setPage(value);
  buttonbar->clearFocus();
}

/**
 * \short Slot function for teleTextChannelChanged()
 * 
 * \param value The value given.
 * 
 * Handles the teleTextChannelChanged() signal from the ButtonBar
 */

void Controller::teleTextChannelChanged(const QString& value) {
  pageinfo->setTeletextChannel(value);
  qDebug() << "teleTextChannelChanged(" << value << ");";
}
